package com.sys.servlet;

import com.sys.util.console;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("unused")
public class sysServlet extends HttpServlet {
  /**
   * 正则表达式：验证用户名
   */
  protected static final String UserNameRule = "^[0-9]\\w{12,12}$";

  /**
   * 正则表达式：验证密码
   */
  protected static final String PassWordRule = "^[a-zA-Z0-9]{6,20}$";

  /**
   * 正则表达式：验证手机号
   */
  static final String IphoneRule = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

  /**
   * 正则表达式：验证邮箱
   */
  static final String EmailRule = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

  /**
   * 正则表达式：验证汉字且长度>= 2
   */
  protected static final String ChineseRule = "[\\u4E00-\\u9FA5]{2,}";

  /**
   * 正则表达式：验证身份证
   */
  protected static final String IdentificationRule = "(^\\d{18}$)|(^\\d{15}$)";

  /**
   * 正则表达式：验证URL
   */
  protected static final String URLRule = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

  /**
   * 正则表达式：验证IP地址
   */
  protected static final String IPRule = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";

  /**
   * 正则表达式：验证IP地址
   */
  protected static final String HomeAddressRule = "^(?=.*?[\\u4E00-\\u9FA5])[\\dA-Za-z\\u4E00-\\u9FA5]+";

  protected static final long serialVersionUID = 1L;

  // 上传文件存储目录
  static final String UPLOAD_DIRECTORY = "upload";

  private static final String SERVER_NAME = "beautyServer";

  // 上传配置
  static final int MEMORY_THRESHOLD = 1024 * 1024 * 3;  // 3MB
  static final int MAX_FILE_SIZE = 1024 * 1024 * 100; // 100MB
  static final int MAX_REQUEST_SIZE = 1024 * 1024 * 200; // 200MB

  static final String uploadBasePath = File.separator + "tmp" + File.separator + "picServer" + File.separator + SERVER_NAME;

  boolean checkLegal(String Str, String Rule) {
    //正则表达式模式
    Pattern p = Pattern.compile(Rule);
    //正则表达式的匹配器
    Matcher m = p.matcher(Str);
    //返回boolean
    return m.matches();
  }

  void setHeadHtml(HttpServletRequest req, HttpServletResponse resp, String setName) throws UnsupportedEncodingException {
    /* 允许跨域的请求头 */
    resp.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");
    /* 允许跨域的请求方法GET, POST, HEAD 等 */
    resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    /* 允许跨域的主机地址 */
    resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
    /* 重新预检验跨域的缓存时间 (s) */
    resp.setHeader("Access-Control-Max-Age", "0");
    /* 是否携带cookie */
    resp.setHeader("Access-Control-Allow-Credentials", "true");
    resp.setHeader("XDomainRequestAllowed", "1");
    req.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=utf-8");
    resp.setHeader("pragma", "no-cache");
    resp.setHeader("cache-control", "no-cache");
    console.log("------------|" + setName + "|---------------");
  }

}
